﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class OfferControl : UserControl
    {
        Offers offer = new Offers();
        int Project_ID;
        string Project_Name;
        string Project_Partner="0";
        Users ActiveUser = Program.GetActiveUser;

        public OfferControl()
        {
            InitializeComponent();
        }

        public OfferControl(DataGridViewRow parameters)
        {
            InitializeComponent();
            Project_ID = Convert.ToInt32(parameters.Cells[0].Value);
            Project_Name = parameters.Cells[1].Value.ToString();
            Project_Partner = parameters.Cells[2].Value.ToString();
        }

        private void OfferControl_Load(object sender, EventArgs e)
        {
            dgvOffresList.DataSource = offer.GetOffersForProject(Project_ID).Tables[0];
            
            
            //dgvOffresList.Columns[0].DisplayIndex = 1;
            dgvOffresList.Columns[8].DisplayIndex = 0;
            dgvOffresList.Columns[9].DisplayIndex = 1;
            dgvOffresList.Columns[10].DisplayIndex = 2;
            dgvOffresList.Columns[5].DisplayIndex = 4;
            dgvOffresList.Columns[7].DisplayIndex = 5;
            //dgvOffresList.Columns[8].DisplayIndex = 0;
            dgvOffresList.Columns[8].HeaderText = "Понуда бр.";
            dgvOffresList.Columns[9].HeaderText = "Проект";
            dgvOffresList.Columns[0].Visible = false;
            dgvOffresList.Columns[1].Visible = false;
            dgvOffresList.Columns[6].Visible = false;
            dgvOffresList.Columns[2].HeaderText = "Датум";
            dgvOffresList.Columns[3].HeaderText = "Цена";
            dgvOffresList.Columns[4].HeaderText = "Статус";
            dgvOffresList.Columns[5].HeaderText = "ДДВ";
            //dgvOffresList.Columns[6].HeaderText = "Цена со ДДВ";
            dgvOffresList.Columns[7].HeaderText = "Цена со ДДВ";
            dgvOffresList.Columns[10].HeaderText = "Кооперант";
            dgvOffresList.Columns[4].DisplayIndex = 9;
            dgvOffresList.Columns[11].Visible = false;
            dgvOffresList.Columns[12].Visible = false;
            dgvOffresList.Columns[13].Visible = false;
            dgvOffresList.Columns[14].Visible = false;
            dgvOffresList.Columns[15].Visible = false;
            dgvOffresList.Columns[16].Visible = false;
            dgvOffresList.Columns[17].HeaderText = "Одговорен";
            dgvOffresList.Columns[18].Visible = false;




        }

        private void btnNewOffer_Click(object sender, EventArgs e)
        {
            
        }

        private void dgvOffresList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow parametars = new DataGridViewRow();
            DataGridView dgvtmp = new DataGridView();

            parametars = dgvOffresList.CurrentRow;
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_OFFERS_CONTROL, parametars);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_OFFERS_CONTROL, parametars);
            this.Dispose();
        }

        private void btnAcepte_Click(object sender, EventArgs e)
        {
            
        }

        private void btnReject_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            DataGridViewRow parametars = new DataGridViewRow();
            DataGridView dgvtmp = new DataGridView();
            dgvtmp.ColumnCount = 3;
            dgvtmp.RowCount = 1;
            dgvtmp.Rows[0].Cells[0].Value = Convert.ToInt32(Project_ID);
            dgvtmp.Rows[0].Cells[1].Value = Project_Name;
            dgvtmp.Rows[0].Cells[2].Value = Project_Partner;

            parametars = dgvtmp.Rows[0];
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_OFFERS_CONTROL, parametars);
            this.Dispose();
        }

        private void ToStAcepted_Click(object sender, EventArgs e)
        {
            if (dgvOffresList.SelectedRows.Count > 0)
                if ((ActiveUser.GetName() + " " + ActiveUser.GetSurname()).ToString() == dgvOffresList.SelectedRows[0].Cells[17].Value.ToString())
                
                    if (dgvOffresList.SelectedRows[0].Cells[4].Value.ToString() == "Пратена")
                    {
                        offer.CloseOffer(dgvOffresList.SelectedRows[0].Cells[0].Value.ToString(), "Прифатена");
                    }
                    else MessageBox.Show("Немате селектирано понуда");
                else MessageBox.Show("Понудата е веќе прифатена или одбина");
            else MessageBox.Show("Не сте одговорен за избраната понуда");
            OfferControl_Load(sender, e);
        }

        private void ToStRegected_Click(object sender, EventArgs e)
        {
            if (dgvOffresList.SelectedRows.Count > 0)
                if ((ActiveUser.GetName() + " " + ActiveUser.GetSurname()).ToString() == dgvOffresList.SelectedRows[0].Cells[17].Value.ToString())
                    if (dgvOffresList.SelectedRows[0].Cells[4].Value.ToString() == "Пратена")
                    {
                        offer.CloseOffer(dgvOffresList.SelectedRows[0].Cells[0].Value.ToString(), "Одбиена");
                    }
                    else MessageBox.Show("Немате селектирано понуда");
                else MessageBox.Show("Понудата е веќе прифатена или одбина");
            else MessageBox.Show("Не сте одговорен за избраната понуда");
            OfferControl_Load(sender, e);
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }
    }
}
